package cfs.databean;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

/**
 * 
 * @author danny seow
 */
public class FundPriceHistoryManager {
	ArrayList<FundPriceHistory> fundPriceList = new ArrayList<FundPriceHistory>();

	public String getMaximumOverPeriod(ArrayList<FundPriceHistory> fundList,
			int period) {
		double maximumPrice = 0;
		fundPriceList = fundList;
		sortFundList();
		int y = 0;
		for (int x = period; x <= fundPriceList.size(); x--) {
			if (y == period) {
				break;
			}
			if (maximumPrice < fundPriceList.get(y).getPrice()) {
				maximumPrice = fundPriceList.get(y).getPrice();
			}
			System.out.println("maximumPrice" + maximumPrice);
			System.out.println("Date: " + fundPriceList.get(y).getPrice_date());
			y++;
		}
                if(maximumPrice==0){
                    return "Have yet to reached 1 week status;";
                }
		return formatDecimal(maximumPrice);
	}

	public double calculateVioliatity(ArrayList<FundPriceHistory> fundList,
			int period) {
		double totalFundsSum = 0;
		double brackets = 0.0;
		for (int z = 0; z < fundList.size(); z++) {
			double alteredState = fundList.get(z).getPrice() / 100;
			totalFundsSum = totalFundsSum + alteredState;
		}
		double average = totalFundsSum / fundList.size();
		for (int z = 0; z < fundList.size(); z++) {
			double alteredState = fundList.get(z).getPrice() / 100;
			brackets = brackets
					+ (Math.pow((alteredState - average), 2) / (fundList.size() - 1));
		}
		brackets = Math.sqrt(brackets);
		System.out.println("Deviation: " + brackets);
		double violatity = 0;
		switch (period) {
		// If yearly violiatity is chosen
		case 1:
			violatity = brackets * (Math.sqrt(252));
			break;
		// if monthly is chosen
		case 2:
			violatity = ((brackets * (Math.sqrt(252))) * Math.sqrt(0.83333));
			break;
		}
		return violatity;

	}

	public String getMinimumOverPeriod(ArrayList<FundPriceHistory> fundList,
			int period) {
		double minimumPrice = 0;
		fundPriceList = fundList;
		sortFundList();
		int y = 0;
		for (int x = period; x <= fundPriceList.size(); x--) {
			if (y == 0) {
				minimumPrice = fundPriceList.get(y).getPrice();
			}
			if (y == period) {
				break;
			}
			if (minimumPrice > fundPriceList.get(y).getPrice()) {
				minimumPrice = fundPriceList.get(y).getPrice();
			}
			y++;
		}
                if(minimumPrice==0){
                    return "Have yet to reached 1 week status;";
                }
		return formatDecimal(minimumPrice);
	}

	public String getLatestDifference(ArrayList<FundPriceHistory> fundList) {
		double latestPrice = 0;
		double finalPrice = 0;
		fundPriceList = fundList;
		if (fundPriceList.size() > 1) {
			sortFundList();
			for (int z = 1; z >= 0; z--) {
				latestPrice = fundList.get(z).getPrice();
				finalPrice = latestPrice - finalPrice;
			}
		}
		return formatDecimal(finalPrice);
	}

	public HashMap getLatestDifferenceList(FundPriceHistory[] fundPriceList) {
		HashMap priceDifferenceMap = new HashMap();
		ArrayList<FundPriceHistory> forFundA = new ArrayList<FundPriceHistory>();
		for (int z = 0; z < fundPriceList.length; z++) {
			forFundA.clear();
			String differenceResult = "";
			int fundID = fundPriceList[z].getFund_id();
			if (!priceDifferenceMap.containsKey(fundID)) {
				for (int y = 0; y < fundPriceList.length; y++) {
					if (fundPriceList[y].getFund_id() == fundID) {
						forFundA.add(fundPriceList[y]);
					}
				}
				differenceResult = getLatestDifference(forFundA);
				priceDifferenceMap.put(fundID, differenceResult);
				System.out.println("differenceResult: " + differenceResult);
				System.out.println("fundID: " + fundID);
			}
		}
		return priceDifferenceMap;
	}

	public void sortFundList() {
		Collections.sort(fundPriceList, new Comparator() {

			@Override
			public int compare(Object o1, Object o2) {
				FundPriceHistory fund1 = (FundPriceHistory) o1;
				FundPriceHistory fund2 = (FundPriceHistory) o2;
				return fund2.getPrice_date().compareTo(fund1.getPrice_date());
			}
		});
	}

	public ArrayList<FundPriceHistory> sortFundList(
			FundPriceHistory[] fundPriceList) {
		ArrayList<FundPriceHistory> fundPriceListing = (ArrayList<FundPriceHistory>) new ArrayList<FundPriceHistory>(
				Arrays.asList(fundPriceList));
		Collections.sort(fundPriceListing, new Comparator() {
			@Override
			public int compare(Object o1, Object o2) {
				FundPriceHistory fund1 = (FundPriceHistory) o1;
				FundPriceHistory fund2 = (FundPriceHistory) o2;
				return fund2.getPrice_date().compareTo(fund1.getPrice_date());
			}
		});
		return fundPriceListing;
	}

	public String formatDecimal(double price) {
		double finalOutput = price / 100;
		NumberFormat formatter = new DecimalFormat("#,##0.00");
		String format = formatter.format(finalOutput);
		return format;
	}

}
